import rx.Observable;
import rx.Scheduler;
import rx.schedulers.Schedulers;

import java.io.IOException;
import java.util.Arrays;

/**
 * Created by codeczx on 2017/2/27.
 */
public class ThreadTest {

    void testUnSubscribeOn(){
        Observable.using(()->{
            System.out.println("resourceFactory in thread:"+Thread.currentThread().getId());
            return Arrays.asList(1,2);
        },ints->{
            System.out.println("observableFactory in thread:"+Thread.currentThread().getId());
            return Observable.from(ints);
        },ints->{
            System.out.println("unSubscribe in thread:"+Thread.currentThread().getId());
        }).unsubscribeOn(Schedulers.newThread())
                .subscribe(System.out::println);
    }

    void testImmediate(){
        Scheduler scheduler = Schedulers.immediate();
        Scheduler.Worker worker = scheduler.createWorker();
        worker.schedule(()->{
            System.out.println("start");
            worker.schedule(()->System.out.println("inner"));
            System.out.println("end");
        });
    }

    void testNewThread(){
        Scheduler scheduler = Schedulers.newThread();
        Scheduler.Worker worker = scheduler.createWorker();
        worker.schedule(()->{
            System.out.println("start");
            worker.schedule(()->{
                System.out.println("inner");
            });
            System.out.println("end");
        });
        try {
            System.in.read();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        ThreadTest test = new ThreadTest();
//        test.testUnSubscribeOn();
//        test.testImmediate();
        test.testNewThread();
    }
}
